<?php

defined('_JEXEC') or die('Restricted access');
JTable::addIncludePath(JPATH_COMPONENT_ADMINISTRATOR . '/tables');

class  ConsoleModelcampaignchannel  extends JModelList
{
    public function store($inputs)
    {
        $trackDate = JFactory::getDate()->format('Y-m-d');
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->insert('pp_campaign_channel_cfg')
            ->columns(
                array(
                    $db->quoteName('campaign_id'), $db->quoteName('channel_id'),
                    $db->quoteName('cost_src'),  $db->quoteName('cost'),
                    $db->quoteName('enable_sync')
                )
            )
            ->values(' "'.$inputs['campaign_id'].'",  "'.$inputs['channel_id'].'", "'.$inputs['cost_src'].'", "'.$inputs['cost'].'","'.$inputs['enable_sync'].'" ' );

        $db->setQuery($query);

        try
        {
            $insertId = $db->execute();
        }
        catch (RuntimeException $e)
        {
            JError::raiseError(500, $e->getMessage());
        }
         $insertId = $db->insertid();
         if ($insertId && $inputs['map']) {
             $this->syncMap($insertId,$inputs['map']);
         }
        return true;
    }

    public function update($inputs)
    {
        $id = $inputs['id'];
        $db = $this->getDbo();
        $query = $db->getQuery(true)
            ->update('pp_campaign_channel_cfg')
            ->set("cost_src = '".trim($inputs['cost_src'])."'")
            ->set("cost = '".trim($inputs['cost'])."'")
            ->set("enable_sync = '".trim($inputs['enable_sync'])."'")
            ->where('id = ' . (int) $id);

        // Set the query and execute the update.
        $db->setQuery($query);

        try
        {
            $db->execute();
        }
        catch (RuntimeException $e)
        {
            JError::raiseError(500, $e->getMessage());
        }

        $this->deleteMap($id);
        if ($inputs['map']) {
            $this->syncMap($id,$inputs['map']);
        }
        return true;
    }

    public function delete($id)
    {
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->delete('pp_campaign_channel_cfg')

            ->where('id='.$id);

        $db->setQuery($query);

        try
        {
            $db->execute();
        }
        catch (RuntimeException $e)
        {
            JError::raiseError(500, $e->getMessage());
        }
        $this->deleteMap($id);
        return true;
    }

    /**deleteMap
     * @param $id
     */
    public function deleteMap($campaign_channel_id)
    {
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->delete('pp_campaign_event_mapping')

            ->where('campaign_channel_id='.$campaign_channel_id);

        $db->setQuery($query);

        try
        {
            $db->execute();
        }
        catch (RuntimeException $e)
        {
            JError::raiseError(500, $e->getMessage());
        }
        return true;
    }
    /**sync   campaign_event_mapping
     * @param $campaign_channel_id
     * @param $tag
     */
    public function syncMap($campaign_channel_id, $tag)
    {
        $count = count($tag['event_id']);
        for ($i=0; $i<$count; $i++) {
            $data['campaign_channel_id'] = $campaign_channel_id;
            $data['event_id'] = $tag['event_id'][$i];
            $data['partner_event_id'] = $tag['event_tag'][$i];
            $this->storeMapping($data);
        }
        return true;
    }

    public function storeMapping($inputs)
    {
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->insert('pp_campaign_event_mapping')
            ->columns(
                array(
                    $db->quoteName('campaign_channel_id'),
                    $db->quoteName('event_id'), $db->quoteName('partner_event_id')
                )
            )
            ->values(' "'.$inputs['campaign_channel_id'].'",  "'.$inputs['event_id'].'", "'.$inputs['partner_event_id'].'" ');

        $db->setQuery($query);

        try
        {
            $db->execute();
        }
        catch (RuntimeException $e)
        {
            JError::raiseError(500, $e->getMessage());
        }
        return true;
    }

    public function getItem($id = null)
    {
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->select('*')
            ->from('pp_campaign_channel_cfg')->where('id='.$id);
        $db->setQuery($query);
        $Item= $db->loadObject();
        return $Item;
    }

    public function getCampaigEventMapping($campaign_channel_id)
    {
        $db = $this->getDbo();
        $query = $db->getQuery(true);
        $query->select('campaign.*,event.event_name')
            ->from('pp_campaign_event_mapping campaign')
            ->join('INNER', 'pp_event_cfg event on event.event_id=campaign.event_id')
            ->where('campaign.campaign_channel_id='.$campaign_channel_id);
        $db->setQuery($query);
        $list = $db->loadObjectList();
        return $list;
    }
}